Page({
  data: {
    username: '',
    password: '',
    phone: '',
    realName: ''
  },
  getUsername(e) {
    this.setData({ username: e.detail.value })
    console.log(e.detail.value)
  },
  getPassoword(e) {
    this.setData({ password: e.detail.value })
  },
  getPhoneNum(e) {
    this.setData({ phone: e.detail.value })
  },
  getRealName(e) {
    this.setData({ realName: e.detail.value })
  },

  

  register() {
    let username = this.data.username
    let realName = this.data.realName
    let password = this.data.password
    let phone = this.data.phone
    if (username.length < 6 || username.length > 14) {
      console.log('用户名格式错误'+username)
      wx.showToast({
        icon: 'none',
        title: '账号必须大于6位且小于14位',
      })
      return
    }
    if (password.length < 6 || password.length > 14) {
      wx.showToast({
        icon: 'none',
        title: '密码必须大于6位且小于14位',
      })
      return
    }
    var phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!phoneReg.test(phone)) {
      wx.showToast({
        icon: 'none',
        title: '手机号码格式错误',
      })
      return
    }
    if (realName.length < 2 || realName.length > 10) {
      wx.showToast({
        icon: 'none',
        title: '真实姓名格式错误',
      })
      return
    }
    wx.request({
      url: 'http://120.77.156.205:8808/wx/ucenter/register',
      data: {
        username: username,
        password: password,
        phone: phone,
        realName: realName
      },
      method: 'POST',
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      success: (res) => {

        if (res.data.code === 5) {
          wx.showToast({
            icon: 'error',
            title: '该账号已被注册！'
          })
        } else {
          wx.showToast({
            icon: 'success',
            title: '注册成功'
          })
          wx.setStorageSync('username', this.data.username)
          wx.setStorageSync('password', this.data.password)
          wx.setStorageSync('phone', this.data.phone)
          wx.setStorageSync('realName', this.data.realName)
          wx.reLaunch({
            url: '../../pages/index/index',
          })
        }

      },
      fail: (e) => {
        console.log(e)
      }
    })
  }

})